/**
 * 
 */
package ikeuilib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * @author ikechan
 *
 */
public class TableParam extends TableParamBasic {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * @param model
	 */
	private DefaultTableModel Mdl;
	public TableParam(DefaultTableModel model) {
		super(model);
		Mdl=model;
	}
	/**
	 * テーブルにオブジェクトをセットする
	 * @param item 名前
	 * @param val　値
	 */
	public void addValue(String item, Object val){
		int r = getRowCount();
		Mdl.setRowCount(r+1);
		setValueAt(item,r,0);
		setValueAt(val,r,1);
	}
	/**
	 * 
	 * 専用につかうテーブルモデル
	 * @author ikechan
	 *
	 */
	public static class MyModel extends DefaultTableModel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		public MyModel(){
			super();
			setColumnCount(2);
			setColumnIdentifiers(new String[]{ "Item", "Value"});
		}
		@Override
		public boolean isCellEditable(int arg0, int arg1) {
			return (arg1==1);
		}
	}
	
	public static void main(String[] arg){
		EventQueue.invokeLater( new Runnable() {
			TableParam me;
			@Override
			public void run() {
				JFrame frame = new JFrame();
				frame.setSize( 400, 300 );
				frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
				frame.setLocationByPlatform( true );
				frame.setTitle( "Table Param test " );
				frame.setVisible(true);
				
				//　インスタンス化して 
				me = new TableParam(new TableParam.MyModel());
				
				JScrollPane scroll = new JScrollPane(me);
				JPanel p = new JPanel();
				p.setLayout(new BorderLayout());
				p.add(scroll,BorderLayout.CENTER);
				JButton btn = new JButton("OK");
				p.add(btn,BorderLayout.SOUTH);
				frame.add(p);
				
				// item と　valをセットする
				me.addValue("TestBoolean1", true);
				me.addValue("TestBoolean2", false);
				me.addValue("TestStr", "Good");
				me.addValue("TestInt", 123);
				me.addValue("Double", 123.0);
				me.addValue("Combo",new JComboBox(new String[]{"A","b","Cd"}));
				me.addValue("Color",new Color(0xcccccc)); 
				me.addValue("Font",new Font("Dialog",Font.PLAIN,12)); 
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						// すべてを列挙してみる
						int max = me.getRowCount();
						for(int i=0;i<max;i++){
							Object o = me.getValue(i);
							System.out.println("Index = " + i + " Object " + o);
						}
					}
					
				});
				
			}
			
		});
	}

}
